/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.demo;

import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>Project: app2022 - Stream6
 * <p>Powered by webrx On 2022-01-26 14:29:47
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Stream6 {
    public static void main(String[] args) {
        var is = Stream.of(
                new Student(1, "李四", "男", 18),
                new Student(22, "James Gosling", "男", 68),
                new Student(2, "张三", "男", 28),
                new Student(3, "王五", "男", 38),
                new Student(1, "李四", "男", 18),
                new Student(4, "李丽", "女", 25)
        );

        //IntStream iii = is.mapToInt(Student::getAge);
        //System.out.println(iii.count());

        //收集ArrayList<Student>
        //List<Student> list = is.collect(Collectors.toList());
        //System.out.println(list);

        //收集ArrayList<String>
        //List<String> ns = is.map(Student::getName).collect(Collectors.toList());
        //System.out.println(ns);

        //count统计个数
        //System.out.println(is.filter(e -> "女".equals(e.getGender())).count());
        //System.out.println(is.filter(e -> "女".equals(e.getGender())).collect(Collectors.counting()));
        //System.out.println(is.filter(e -> e.getAge() <= 18).collect(Collectors.counting()));

        //求年龄最大的信息(Student) maxBy(升序）求最大  minBy(升序）求最小  max(降序）是求最小
        //System.out.println(is.collect(Collectors.maxBy((a, b) -> a.getAge() - b.getAge())).get());
        //求出最小年龄值是多少岁
        //System.out.println(is.collect(Collectors.maxBy((a, b) -> b.getAge() - a.getAge())).get().getAge());

        //收集所有学生年龄总和，是一个int数字
        //System.out.println(is.collect(Collectors.summingInt(Student::getAge)));

        //平均年龄  32.5
        //System.out.println(is.collect(Collectors.averagingInt(Student::getAge)));

        //一个次统计出所有信息 个数，最大，最小，平均，求和
        var iss = is.collect(Collectors.summarizingInt(Student::getAge));
        System.out.println(iss.getSum());
        System.out.println(iss.getAverage());
        System.out.println(iss.getMax());
        System.out.println(iss.getMin());
        System.out.println(iss.getCount());
    }
}
